﻿using PBO.Network;
using PBO.Network.Enumerations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PBO.Forms.NetworkForm
{
    public partial class DirectBattleForm : Form
    {
        private MainForm motherForm = MainForm.GetInstance();

        public DirectBattleForm()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.PokemonBall;
        }

        private void DirectBattleForm_Load(object sender, System.EventArgs e)
        {
            //motherForm = (MainForm)MdiParent;
            ModeCombo.SelectedIndex = 0;
        }

        private void ServerRadio_CheckedChanged(object sender, System.EventArgs e)
        {
            if (ServerRadio.Checked)
            {
                AddressText.ReadOnly = true;
                RuleGroup.Enabled = true;
            }
            else
            {
                AddressText.ReadOnly = false;
                RuleGroup.Enabled = false;
            }
        }

        private void ObseverRadio_CheckedChanged(object sender, System.EventArgs e)
        {
            if (ObseverRadio.Checked)
            {
                NameText.ReadOnly = true;
                ModeCombo.Enabled = false;
            }
            else
            {
                NameText.ReadOnly = false;
                ModeCombo.Enabled = true;
            }
        }

        private void OKButton_Click(object sender, System.EventArgs e)
        {
            if (NameText.ReadOnly == false && string.IsNullOrEmpty(NameText.Text))
                return;
            if (AddressText.ReadOnly == false && string.IsNullOrEmpty(AddressText.Text))
                return;

            if (ModeCombo.SelectedIndex != 0 && motherForm.Team.Pokemons[1].Identity == 0 && !ObseverRadio.Checked)
            {
                MessageBox.Show("2V2至少要有两只PM");
                return;
            }
            if (ServerRadio.Checked)
            {
                if (ModeCombo.SelectedIndex == 2)
                {
                    Form form = motherForm.Build4PServerForm(NameText.Text, Rules.ToArray());
                    form.MdiParent = this.MdiParent;
                    form.Show();
                }
                else
                {
                    Form battleForm = motherForm.BuildBattleServerForm(NameText.Text, BattleMode, Rules.ToArray());
                    battleForm.Show();
                }
            }
            else if (ClientRadio.Checked)
            {
                if (ModeCombo.SelectedIndex == 2)
                {
                    Form form = motherForm.Build4PClientForm(AddressText.Text, NameText.Text);
                    form.Show();
                }
                else
                {
                    Form battleForm = motherForm.BuildBattleClientForm(AddressText.Text, 2, NameText.Text, BattleMode);
                    battleForm.Show();
                }
            }
            else
            {
                Form form = motherForm.BuildBattleObserverForm(AddressText.Text, 1);
                form.Show();
            }
        }

        private BattleMode BattleMode
        {
            get
            {
                switch (ModeCombo.SelectedIndex)
                {
                    case 0:
                        return BattleMode.Single;
                    case 1:
                        return BattleMode.Double;
                    case 2:
                        return BattleMode.Double_4P;
                    default:
                        return BattleMode.Single;
                }
            }
        }

        private IEnumerable<BattleRule> Rules
        {
            get
            {
                if (PPUpCheck.Checked) yield return BattleRule.PPUp;
                if (RandomCheck.Checked) yield return BattleRule.Random;
            }
        }

        private void Cancel_Button_Click(object sender, System.EventArgs e)
        {
            Close();
        }
    }
}
